import 'package:cbec_app/entity/home/config_data.dart';
import 'package:cbec_app/entity/home/content/advance_sale_data.dart';
import 'package:cbec_app/entity/home/content/bargain_activity_data.dart';
import 'package:cbec_app/entity/home/content/cateslide_group_entity.dart';
import 'package:cbec_app/entity/home/content/content_data.dart';
import 'package:cbec_app/entity/home/content/coupon_decoration_entity.dart';
import 'package:cbec_app/entity/home/content/cube_model_data.dart';
import 'package:cbec_app/entity/home/content/goods_data_model.dart';
import 'package:cbec_app/entity/home/content/groupon_activity_data.dart';
import 'package:cbec_app/entity/home/content/image_ad_hot_area_data.dart';
import 'package:cbec_app/entity/home/content/image_text_nav_entity.dart';
import 'package:cbec_app/entity/home/content/lotter_home_data.dart';
import 'package:cbec_app/entity/home/content/new_image_ad_entity.dart';
import 'package:cbec_app/entity/home/content/new_zone_data.dart';
import 'package:cbec_app/entity/home/content/notice_entity.dart';
import 'package:cbec_app/entity/home/content/red_packet_decoration_entity.dart';
import 'package:cbec_app/entity/home/content/rich_text_data.dart';
import 'package:cbec_app/entity/home/content/scroll_image_slider_entity.dart';
import 'package:cbec_app/entity/home/content/search_content_data.dart';
import 'package:cbec_app/entity/home/content/sec_kill_data.dart';
import 'package:cbec_app/entity/home/content/special_price_data.dart';
import 'package:cbec_app/entity/home/content/superior_special_price_entity.dart';
import 'package:cbec_app/entity/home/content/text_title_entity.dart';
import 'package:cbec_app/entity/home/content/topic_product_entity.dart';
import 'package:cbec_app/entity/home/content/video_content_data.dart';
import 'package:cbec_app/entity/home/content/white_line_dart.dart';
import 'package:cbec_app/entity/home/navigation_data.dart';
import 'package:cbec_app/utils/logger.dart';

import 'content/com_title06_entity.dart';
import 'content/group_goods_data_entity.dart';
import 'content/sign_in_home_data.dart';
import 'hom_nav_tab_entity.dart';

class HomeDataModel {
  final String? id; // 唯一标识符
  final String? cd; // 代码
  final String? md; // 模板
  final int? df; // 数据字段
  final String? cid; // 内容标识符
  final String? sid; // 会话标识符
  final int? status; // 状态
  final int? type; // 类型
  final String? name; // 名称
  final String? fileUrl; // 文件链接
  final String? startDate; // 开始日期
  final String? endDate; // 结束日期
  final int? mainPage; // 主页面
  final NavigationData? navigation; // 导航数据
  final List<ContentData>? content; // 内容列表
  final ContentNavEntity? navContent; // 导航内容
  final ConfigData? config; // 配置数据

  HomeDataModel({
    this.id,
    this.cd,
    this.md,
    this.df,
    this.cid,
    this.sid,
    this.status,
    this.type,
    this.name,
    this.fileUrl,
    this.startDate,
    this.endDate,
    this.mainPage,
    this.navigation,
    this.content,
    this.navContent,
    this.config,
  });

  factory HomeDataModel.fromJson(Map<String, dynamic> json) {
    int type = json['type'];
    List<ContentData>? contentList;
    ContentNavEntity? navContent;

    // 根据 type 值解析不同的内容
    if (type == 1) {
      contentList = (json['content'] as List)
          .map((item) {
            try {
              var model = _createContentData(item);
              return model;
            } catch (e) {
              print('解析内容项时出错: $e   $item');
              return null; // 返回 null 表示解析失败
            }
          })
          .whereType<ContentData>()
          .toList(); // 过滤掉 null 值
    } else if (type == 2) {
      navContent = ContentNavEntity.fromJson(json['content']);
    }

    return HomeDataModel(
      id: json['id'],
      cd: json['cd'],
      md: json['md'],
      df: json['df'],
      cid: json['cid'],
      sid: json['sid'],
      status: json['status'],
      type: json['type'],
      name: json['name'],
      fileUrl: json['fileUrl'],
      startDate: json['startdate'],
      endDate: json['enddate'],
      mainPage: json['mainPage'],
      navigation: NavigationData.fromJson(json['navigation']),
      content: contentList,
      navContent: navContent,
      config: ConfigData.fromJson(json['config']),
    );
  }

  static ContentData _createContentData(Map<String, dynamic> json) {
    switch (json['type']) {
      case 'title_text':
        return TextTitleEntity.fromJson(json);
      case 'rich_text':
        return RichTextData.fromJson(json);
      case 'white_line':
        return WhiteLineModel.fromJson(json);
      case 'new_image_ad':
        return NewImageAdEntity().fromJson(json);
      case 'image_ad_hot_area':
        return ImageAdHotAreaModel.fromJson(json);
      case 'cube':
        return CubeModel.fromJson(json);
      case 'search':
        return SearchContentModel.fromJson(json);
      case 'image_text_nav':
        return ImageTextNavEntity.fromJson(json);
      case 'video':
        return VideoModel.fromJson(json);
      case 'notice':
        return NoticeEntity.fromJson(json);
      case 'goods':
        return GoodsDataModel.fromJson(json);
      case 'goods_group':
        return GroupGoodsDataEntity.fromJson(json);
      case 'new_zone':
        return NewZoneData.fromJson(json);
      case 'ump_limitdiscount':
        return SpecialPriceData.fromJson(json);
      case 'ump_seckill':
        return SeckillData.fromJson(json);
      case 'bargain':
        logger.i('看看砍价');
        return BargainActivity.fromJson(json);
      case 'groupon':
        return GrouponActivity.fromJson(json);
      case 'advance_sale':
        return AdvanceSaleContent.fromJson(json);
      case 'comtitle06':
        logger.i('comtitle06');
        return ComTitle06Entity.fromJson(json);
      case 'cateslide': // 滑动分组
        print('滑动分组 ====');
        return CateslideGroupEntity.fromJson(json);
      case 'topicitems': // 主题商品A1
        return TopicProductEntity.fromJson(json);
      case 'limitdiscount01': // 特价组件A1
        logger.i('特价组件A1');
        return SuperiorSpecialPriceEntity().fromJson(json);
      case 'imgslide01': // 轮播组合01
        return ScrollImageSliderEntity.fromJson(json);
      case 'raffle':
        return LotterHomeData.fromJson(json);
      case 'signin':
        return SignInHomeData.fromJson(json);
      case 'red_packet':
        return RedPacketDecorationEntity.fromJson(json);
      case 'coupon':
        return CouponDecorationEntity.fromJson(json);
      // 添加其他类型的处理逻辑
      default:
        throw Exception('未知的内容类型: ${json['type']}');
    }
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'cd': cd,
      'md': md,
      'df': df,
      'cid': cid,
      'sid': sid,
      'status': status,
      'type': type,
      'name': name,
      'fileUrl': fileUrl,
      'startdate': startDate,
      'enddate': endDate,
      'mainPage': mainPage,
      'navigation': navigation?.toJson(),
      'content': content?.map((item) => item.toJson()).toList(),
      'config': config?.toJson(),
    };
  }
}
